package com.hfzy.ihk.web.userCenter.controller;



import com.hfzy.ihk.facade.userCenter.entity.UserInfo;
import com.hfzy.ihk.facade.userCenter.service.IUserService;
import com.hfzy.ihk.web.oauth.client.utils.UserUtil;
import com.hfzy.ihk.web.userCenter.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;

import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 用于集成spring security 前端控制器
 * </p>
 *
 * @author chenzesen
 * @since 2018-12-28
 */
@Controller
@Slf4j
public class LoginController {

    @Autowired
    IUserService userService;

    @Autowired
    UserMapper userMapper;

    @Value("${server.servlet.context-path}")
    private String serverPath;

    @RequestMapping(value = {"/main"})
    public ModelAndView pageMain(HttpServletRequest request, HttpServletResponse response) {

        return new ModelAndView("page/main");
    }
    @RequestMapping(value = {"/index"})
    public ModelAndView pageIndex(HttpServletRequest request, HttpServletResponse response,ModelAndView modelAndView) {



        //这里返回白名单，用于遍历退出
        UserInfo userInfo = UserUtil.getLoginUser();
        modelAndView.addObject("userInfo",userInfo);
        String clientId = UserUtil.getLoginclientId()!=null?UserUtil.getLoginclientId():"ihksso";
        List<String> logoutList = userMapper.queryLogoutUrlByclientId(clientId);
        List<String> myLogoutUri = new ArrayList<>();
        for(String s:logoutList){
            String[] args = s.split(",");
            for(String s1:args) {
                if (s1 != null && !s1.contains(serverPath)) {
                    myLogoutUri.add(s1.replace("login","clearSession"));
                }
            }
        }
        modelAndView.addObject("myLogoutUri",myLogoutUri);
        modelAndView.setViewName("page/index");

        return modelAndView;
    }




    /**
     * 登出处理
     */
    @RequestMapping("/logout")
    public ModelAndView logoutView(HttpServletRequest request, HttpServletResponse response){
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        if (auth != null){
            new SecurityContextLogoutHandler().logout(request, response, auth);
        }
        return new ModelAndView("redirect:/loginPage");
    }



}
